package cn.iocoder.yudao.module.wallet.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.MessageResult;
import cn.iocoder.yudao.module.wallet.config.WalletConfig;
import cn.iocoder.yudao.module.wallet.controller.app.request.CreateWalletRequest;
import cn.iocoder.yudao.module.wallet.controller.app.response.WalletResponse;
import cn.iocoder.yudao.module.wallet.service.wallet.WalletService;
import cn.iocoder.yudao.module.wallet.utils.Bip44Util;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.crypto.MnemonicCode;
import org.bitcoinj.crypto.MnemonicException;
import org.bitcoinj.wallet.DeterministicSeed;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.web3j.crypto.Bip39Wallet;
import org.web3j.crypto.Bip44WalletUtils;
import org.web3j.crypto.CipherException;
import org.web3j.protocol.core.methods.response.EthBlockNumber;

import javax.sql.rowset.serial.SerialException;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author liutao
 * @version 1.0
 * @description: TODO
 * @date 2024/10/30 13:18
 */
@Slf4j
@Tag(name = "钱包 App - 钱包管理")
@RestController
@RequestMapping("/wallet")
@Validated
public class AppWalletController {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private WalletService walletService;

    /**
     * Bip44创建助记词
     * <p>
     * BTC使用m/44’/0’/0’/0的 Extended Public Key 生成 m/44’/0’/0’/0/*，
     * ETH使用m/44’/60’/0’/0的 Extended Public Key 生成 m/44’/60’/0’/0/*
     * </p>
     *
     * @return
     */
    @PermitAll
    @PostMapping("/create/mnemonic")
    public CommonResult<String> createMnemonic(@RequestParam("password") String password) {
        return CommonResult.success(walletService.createMnemonic(password));
    }

    @PermitAll
    @PostMapping("/create")
    public CommonResult<List<WalletResponse>> createWallet(@Validated @RequestBody CreateWalletRequest request) {
        List<String> coin = request.getCoin();
        if (CollUtil.isEmpty(coin)) {
            return CommonResult.error(500, "请选择币种");
        }
        List<WalletResponse> list = walletService.importMnemonic(request);
        return CommonResult.success(list);
    }

    @PermitAll
    @GetMapping("/getBlockNumber")
    public CommonResult<BigInteger> getBlockNumber() {
        return CommonResult.success(walletService.getBlockNumber());

    }
}
